package com.ibeeking.found.uaa.s.service.impl;

import com.ibeeking.found.auth.api.common.dto.LoginUserDTO;
import com.ibeeking.found.auth.api.common.query.AccountLoginQuery;
import com.ibeeking.found.auth.api.feign.ShopLoginClient;
import com.ibeeking.found.common.config.feign.MsgFeignClient;
import com.ibeeking.found.common.enums.LoginRoleEnum;
import com.ibeeking.found.uaa.s.model.ShopUserDetails;
import com.ibeeking.found.uaa.s.service.IShopUserDetailsService;
import com.ibeeking.nematos.exception.AuthorizationException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.data.StringUtils;
import com.ibeeking.nematos.utils.result.ResponseResult;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author ibeeking
 */
@Service
public class ShopUserDetailsServiceImpl implements IShopUserDetailsService {

    @Resource
    private ShopLoginClient shopLoginClient;
    @Resource
    private MsgFeignClient msgClient;

    @Override
    public ShopUserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (StringUtils.isBlank(username)) {
            throw new AuthorizationException(HttpStatus.UNAUTHORIZED.value(), "手机号不能为空");
        }
        AccountLoginQuery query = new AccountLoginQuery();
        query.setMobile(username);
        return querySLoginUser(query);
    }

    @Override
    public ShopUserDetails loadUserByPhoneAndSmsCode(String phone, String smsCode, String key) {
        if (StringUtils.isBlank(phone)) {
            throw new AuthorizationException(HttpStatus.UNAUTHORIZED.value(), "手机号不能为空");
        }
        if (StringUtils.isBlank(smsCode)) {
            throw new AuthorizationException(HttpStatus.UNAUTHORIZED.value(), "手机验证码不能为空");
        }
        if (StringUtils.isBlank(key)) {
            throw new AuthorizationException(HttpStatus.UNAUTHORIZED.value(), "验证码标识不能为空");
        }
        ResponseResult<Boolean> responseResult = msgClient.checkVerificationCode(key, phone, smsCode);
        if (null == responseResult || !responseResult.getData()) {
            throw new AuthorizationException(HttpStatus.UNAUTHORIZED.value(), "验证码错误或已过期");
        }
        AccountLoginQuery query = new AccountLoginQuery();
        query.setMobile(phone);
        return querySLoginUser(query);
    }

    /***
     * 封装查询登录用户
     * @param query 参数
     * @return
     */
    private ShopUserDetails querySLoginUser(AccountLoginQuery query) {
        //查询商铺用户
        ResponseResult<LoginUserDTO> responseResult = shopLoginClient.queryAccountLogin(query);
        if (null == responseResult || null == responseResult.getData() || !responseResult.getData().getJob()) {
            throw new AuthorizationException(HttpStatus.UNAUTHORIZED.value(), "用户名或密码错误");
        }
        LoginUserDTO data = responseResult.getData();
        if (data.getLock()) {
            throw new AuthorizationException(HttpStatus.UNAUTHORIZED.value(), "账号已锁定，禁止登录");
        }
        ShopUserDetails shopUserDetails = BeanUtil.convertBean(data, ShopUserDetails.class);
        shopUserDetails.setUserCode(query.getMobile());
        if (Collections3.isEmpty(data.getIds()) || data.getIds().size() > 1) {
            shopUserDetails.setJump(false);
        } else {
            shopUserDetails.setJump(true);
            shopUserDetails.setTenantId(String.valueOf(data.getTenantId()));
            shopUserDetails.setShopId(String.valueOf(data.getShopId()));
        }
        List<SimpleGrantedAuthority> roles = new ArrayList<>();
        roles.add(new SimpleGrantedAuthority(LoginRoleEnum.SHOP.name()));
        roles.add(new SimpleGrantedAuthority(responseResult.getData().getLoginRole()));
        shopUserDetails.setRoles(roles);
        return shopUserDetails;
    }
}
